package com.epsi.mightymaps.hdl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import com.epsi.mightymaps.act.SendCourseActivity;

import android.app.AlertDialog.Builder;
import android.view.View;
import android.view.View.OnClickListener;

public class SendCourseAction implements OnClickListener {
	SendCourseActivity sendCourseActivity;

	public SendCourseAction(SendCourseActivity sendCourseActivity) {
		this.sendCourseActivity = sendCourseActivity;
	}

	public void onClick(View v) {
		URL wsUrl = null;
		try {
			wsUrl = new URL("http://localhost/mightymaps/testsend.php");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			Builder dialogBuilder = new Builder(sendCourseActivity);
			dialogBuilder.setMessage("MalformedURLException 1 : "
					+ e.getMessage());
			dialogBuilder.create().show();
		} catch (Exception e) {
			// TODO: handle exception
			Builder dialogBuilder = new Builder(sendCourseActivity);
			dialogBuilder.setMessage("Exception 1 : " + e.getMessage());
			dialogBuilder.create().show();

		}
		URLConnection connection = null;
		try {
			connection = wsUrl.openConnection();
			connection.setDoOutput(true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Builder dialogBuilder = new Builder(sendCourseActivity);
			dialogBuilder.setMessage("IOException 1 : " + e.getMessage());
			dialogBuilder.create().show();
		} catch (Exception e) {
			// TODO: handle exception
			Builder dialogBuilder = new Builder(sendCourseActivity);
			dialogBuilder.setMessage("Exception 2 : " + e.getMessage());
			dialogBuilder.create().show();

		}
		String data = null;
		try {
			data = URLEncoder.encode("send", "UTF-8") + "="
					+ URLEncoder.encode("test", "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			Builder dialogBuilder = new Builder(sendCourseActivity);
			dialogBuilder.setMessage("UnsupportedEncodingException 1 : "
					+ e.getMessage());
			dialogBuilder.create().show();
		} catch (Exception e) {
			// TODO: handle exception
			Builder dialogBuilder = new Builder(sendCourseActivity);
			dialogBuilder.setMessage("Exception 3 : " + e.getMessage());
			dialogBuilder.create().show();

		}

		OutputStreamWriter wr = null;
		try {
			wr = new OutputStreamWriter(connection.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Builder dialogBuilder = new Builder(sendCourseActivity);
			dialogBuilder.setMessage("IOException 2 : " + e.getMessage());
			dialogBuilder.create().show();
		} catch (Exception e) {
			// TODO: handle exception
			Builder dialogBuilder = new Builder(sendCourseActivity);
			dialogBuilder.setMessage("Exception 4 : " + e.getMessage());
			dialogBuilder.create().show();

		}
		try {
			wr.write(data);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Builder dialogBuilder = new Builder(sendCourseActivity);
			dialogBuilder.setCancelable(true);
			dialogBuilder.setMessage("IOException 3 : " + e.getMessage());
			dialogBuilder.create().show();
		} catch (Exception e) {
			// TODO: handle exception
			Builder dialogBuilder = new Builder(sendCourseActivity);
			dialogBuilder.setCancelable(true);
			dialogBuilder.setMessage("Exception 5 : " + e.getMessage());
			dialogBuilder.create().show();

		}
		try {
			wr.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Builder dialogBuilder = new Builder(sendCourseActivity);
			dialogBuilder.setCancelable(true);
			dialogBuilder.setMessage("IOException 4 : " + e.getMessage());
			dialogBuilder.create().show();
		} catch (Exception e) {
			// TODO: handle exception
			Builder dialogBuilder = new Builder(sendCourseActivity);
			dialogBuilder.setCancelable(true);
			dialogBuilder.setMessage("Exception 6 : " + e.getMessage());
			dialogBuilder.create().show();

		}

		BufferedReader rd = null;
		try {
			rd = new BufferedReader(new InputStreamReader(
					connection.getInputStream()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Builder dialogBuilder = new Builder(sendCourseActivity);
			dialogBuilder.setCancelable(true);
			dialogBuilder.setMessage("IOException 5 : " + e.getMessage());
			dialogBuilder.create().show();
		} catch (Exception e) {
			// TODO: handle exception
			Builder dialogBuilder = new Builder(sendCourseActivity);
			dialogBuilder.setCancelable(true);
			dialogBuilder.setMessage("Exception 7 : " + e.getMessage());
			dialogBuilder.create().show();

		}
		String line;
		String retourString = "";
		try {
			while ((line = rd.readLine()) != null) {
				retourString += line;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Builder dialogBuilder = new Builder(sendCourseActivity);
			dialogBuilder.setCancelable(true);
			dialogBuilder.setMessage("IOException 6 : " + e.getMessage());
			dialogBuilder.create().show();
		} catch (Exception e) {
			// TODO: handle exception
			Builder dialogBuilder = new Builder(sendCourseActivity);
			dialogBuilder.setCancelable(true);
			dialogBuilder.setMessage("Exception 8 : " + e.getMessage());
			dialogBuilder.create().show();

		}
		try {
			wr.close();
			rd.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Builder dialogBuilder = new Builder(sendCourseActivity);
			dialogBuilder.setCancelable(true);
			dialogBuilder.setMessage("IOException 7 : " + e.getMessage());
			dialogBuilder.create().show();
		} catch (Exception e) {
			// TODO: handle exception
			Builder dialogBuilder = new Builder(sendCourseActivity);
			dialogBuilder.setCancelable(true);
			dialogBuilder.setMessage("Exception 9 : " + e.getMessage());
			dialogBuilder.create().show();

		}
		Builder dialogBuilder = new Builder(sendCourseActivity);
		dialogBuilder.setMessage(retourString);
		dialogBuilder.create().show();
	}

}